package com.finace.money.common.util;

import android.provider.Settings;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.TextAppearanceSpan;

import com.finace.money.R;
import com.finace.money.common.BaseSingleton;

import java.math.BigInteger;
import java.security.SecureRandom;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by ZHP on 2017/6/24.
 */
public class StringUtil extends BaseSingleton {

    /**
     * 实例化
     */
    public static StringUtil getInstance() {
        return getSingleton(StringUtil.class);
    }


    /**
     * 判断字符串是 空
     *
     * @param str null、“ ”、“null”都返回true
     * @return
     */
    public static boolean isNullString(String str) {
        return (null == str || str.trim().isEmpty() || "null".equals(str.trim()
                .toLowerCase(Locale.getDefault())));
    }

    /**
     * 判断字符串不是 空
     *
     * @param str null、“ ”、“null”都返回true
     * @return
     */
    public static boolean isNotNullString(String str) {
        return !isNullString(str);
    }

    public static String formatNullString(String str) {
        return isNotNullString(str) ? str : "";
    }

    /**
     * 特殊比较字符串 ""、null、"null"
     *
     * @param lhs
     * @param rhs
     * @return
     */
    public static boolean equalSpecialStr(String lhs, String rhs) {
        if (isNullString(lhs) && isNullString(rhs)) {
            return true;
        } else {
            if (lhs.equals(rhs)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 截取数字
     *
     * @return
     */
    public static String cutNumber(String content) {
        if (isNotNullString(content)) {
            Pattern p = Pattern.compile("[^0-9]");
            Matcher m = p.matcher(content);
            return m.replaceAll("");
        } else {
            return "";
        }
    }

    /*
     *替换日期格式
     * 2016-05-13 --> 2016/05/13
    */
    public static String replaceDate(String Date) {
        return Date.replaceAll("-", "/");
    }


    /**
     * 时间转换
     * 900 --> 09:00
     * 2100 --> 21:00
     */
    public static String changeTime(String start_time) {
        String h;
        String m;
        if (start_time.length() < 4) {
            h = "0" + start_time.substring(0, 1);
            m = start_time.substring(1);
        } else {
            h = start_time.substring(0, 2);
            m = start_time.substring(2);
        }
        return h + ":" + m;
    }

    /**
     * 根据设备生成一个唯一标识
     *
     * @return
     */
    public static String generateOpenUDID() {
        // Try to get the ANDROID_ID
        String OpenUDID = Settings.Secure.getString(UIUtil.getContext().getContentResolver(),
                Settings.Secure.ANDROID_ID);
        if (OpenUDID == null || OpenUDID.equals("9774d56d682e549c")
                | OpenUDID.length() < 15) {
            // if ANDROID_ID is null, or it's equals to the GalaxyTab generic
            // ANDROID_ID or bad, generates a new one
            final SecureRandom random = new SecureRandom();
            OpenUDID = new BigInteger(64, random).toString(16);
        }
        return OpenUDID;
    }

    /**
     * String 转 double 并保留2位小数
     * 6.2041   -->  6.20
     *
     * @param str
     * @return
     */
    /*public static String getDecimal(String str) {

        Double cny = Double.parseDouble(str);//6.2041
        if(Double.MAX_VALUE == cny){
            return "0";
        }else {
            DecimalFormat df = new DecimalFormat("0.00");
            String result = df.format(cny);
            return result + "";
        }

    }*/

    /**
     * String 转 double 并保留2位小数
     * 6.2041   -->  6.20
     *
     * @param d
     * @return
     */
    public static String getDecimal(double d) {
        if (d == Double.MAX_VALUE || d == 1.797693E308) {
            return "0";
        } else {
            DecimalFormat df = new DecimalFormat("0.00");
            String result = df.format(d);
            return result;
        }

    }

    public static String getDecimalMoney(String str) {
        if (StringUtil.isNullString(str)) {
            return null;
        }
        DecimalFormat myformat = new DecimalFormat("###,###");
        return myformat.format(Integer.valueOf(str));
    }


    /**
     * 富文本
     *
     * @param str
     * @param start
     * @param end
     * @return
     */
    public static SpannableString getSpannableString(String str, int start, int end, int style_left, int style_right) {
        SpannableString styledText = new SpannableString(str);
        styledText.setSpan(new TextAppearanceSpan(UIUtil.getContext(), style_left), start, end, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        styledText.setSpan(new TextAppearanceSpan(UIUtil.getContext(), style_right), end, str.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        return styledText;
    }

    /**
     * 加%号
     *
     * @param str
     * @return
     */
    public static String getNumberFormat(String str) {
        if (StringUtil.isNullString(str)) {
            return null;
        }
        NumberFormat percentInstance = NumberFormat.getPercentInstance();
        percentInstance.setMinimumFractionDigits(2);
        return percentInstance.format((Double.valueOf(str)));
    }

    /**
     * 限制最多输入两位小数
     *
     * @param number
     * @return
     */
    public static boolean isOnlyPointNumber(String number) {//保留两位小数正则
        Pattern pattern = Pattern.compile("^\\d+\\.?\\d{0,2}$");
        Matcher matcher = pattern.matcher(number);
        return matcher.matches();
    }


}
